﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
/*    
 * @author Peng Wang
 * @version 1.0.0
*/

namespace vxi.chat.library
{


    [Serializable]
    public class VxiEvent
    {
        public static int VXI_EVT_REF_ID = 0;               ///< 事件引用码

        public class Head 
        {
            public int _domain = 0;
            public int _type = 0;
            public int _event = 0;
            public int _refid = 0;
            public void Reset() 
            {
                _domain = _type = _event = _refid = 0;
            }
        }

        public Head head = new Head();                      ///< 事件体标识

        public Hashtable items = new Hashtable();

        public static int incRefId() 
        {
            VXI_EVT_REF_ID = VXI_EVT_REF_ID > 99999999 ? 1 : (VXI_EVT_REF_ID + 1);
            return VXI_EVT_REF_ID;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public VxiEvent()
        {
            head._refid = incRefId();
        }

        /// <summary>
        /// 事件初始化操作
        /// </summary>
        public void Reset()
        {
            head.Reset();
            items.Clear();
        }

        /// <summary>
        /// 将当前事件转换成字符串格式输出
        /// </summary>
        /// <returns>返回转换结果</returns>
        public String toString() 
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(VxiDefine.Event.Head.DOMAIN).Append("=").Append(head._domain).Append("\n");
            builder.Append(VxiDefine.Event.Head.TYPE).Append("=").Append(head._type).Append("\n");
            builder.Append(VxiDefine.Event.Head.EVENT).Append("=").Append(head._event).Append("\n");
            builder.Append(VxiDefine.Event.Head.REFID).Append("=").Append(head._refid).Append("\n");
            foreach (DictionaryEntry de in items)
            {
                builder.Append(de.Key).Append("=").Append(de.Value).Append("\n");
            }
            return string.Format("{1}\n{0}{2}\n", builder.ToString(),VxiDefine.Event.SocekPackage.BEGIN,VxiDefine.Event.SocekPackage.END);
        }
        
        /// <summary>
        /// 通过字符串加载事件
        /// </summary>
        /// <param name="data">事件字符串</param>
        /// <returns>返回加载结果</returns>
        public bool loadEvent(string source)
        {
            Hashtable hash = new Hashtable();
            source = source.Replace("\r\n", @"\r").Replace(string.Format("{0}\n", VxiDefine.Event.SocekPackage.BEGIN), "").Replace(string.Format("{0}\n", VxiDefine.Event.SocekPackage.END), "");
            
            string[] array = source.Split(("\n").ToArray(),StringSplitOptions.RemoveEmptyEntries);
            foreach (string str in array)
            {
                string[] childArray = str.Split('=');
                hash.Add(childArray.GetValue(0), childArray.GetValue(1));
            }
            return loadEvent(hash);
        }

        /// <summary>
        /// 通过Hashtable加载事件
        /// </summary>
        /// <param name="hash">Hash事件内容</param>
        /// <returns>返回加载结果</returns>
        public bool loadEvent(Hashtable hash)
        {
            this.Reset();
            foreach(DictionaryEntry de in hash)
            {
                if (de.Key.Equals(VxiDefine.Event.Head.DOMAIN))
                {
                    head._domain = int.Parse(de.Value.ToString());
                }
                else if (de.Key.Equals(VxiDefine.Event.Head.TYPE))
                {
                    head._type = int.Parse(de.Value.ToString());
                }
                else if (de.Key.Equals(VxiDefine.Event.Head.EVENT))
                {
                    head._event = int.Parse(de.Value.ToString());
                }
                else if (de.Key.Equals(VxiDefine.Event.Head.REFID))
                {
                    head._refid = int.Parse(de.Value.ToString());
                }
                else
                {
                    items.Add(de.Key, de.Value.ToString());
                }
            }
            return valid();
        }

        /// <summary>
        /// 判断当前事件是否合法
        /// </summary>
        /// <returns>返回判断结果</returns>
        public bool valid()
        {
            return head._type != 0 && head._event != 0;
        }
    }
}
